home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / info-service / www / src / midaswww-1.0 / SGMLFormattedText.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-16  |  18.3 KB  |  553 lines

  1. /*==================================================================*/
  2. /*                                                                  */
  3. /* SGMLFormattedTextObject                                          */
  4. /*                                                                  */
  5. /* T.Johnson - (TonyJ@Slacvx.Slac.Stanford.Edu)           June.92   */
  6. /*                                                                  */
  7. /* Defines a formatted text segment for the SGMLHyper widget        */
  8. /*                                                                  */
  9. /*==================================================================*/
  10.  
  11. #include <X11/IntrinsicP.h>
  12. #include <X11/StringDefs.h>
  13. #include <X11/CoreP.h>
  14. #include "SGMLFormattedTextP.h"
  15.  
  16. /* 
  17.   Private functions 
  18. */
  19.  
  20. #define max(a,b) (a>b?a:b)
  21.  
  22. static void WordSize();
  23. static void LineSize();
  24.  
  25. /*
  26.   Widget class methods
  27. */
  28.  
  29. static void    Initialize();
  30. static void    Destroy();
  31. static void    ComputeSize();
  32. static Boolean AdjustSize();
  33. static void    Redisplay();
  34. static Widget  Contains();
  35. static void    Hilite();
  36.  
  37. #define Offset(field) XtOffsetOf(SGMLFormattedTextRec,sgml_formatted_text.field)
  38.  
  39. static XtResource resources[] = {
  40.  
  41.     {SGMLNrightIndent,SGMLCMargin,XtRDimension,sizeof(int),
  42.     Offset (right_indent),XtRImmediate,(XtPointer)0},
  43.  
  44.     {SGMLNleftIndent,SGMLCMargin,XtRDimension,sizeof(int),
  45.     Offset (left_indent),XtRImmediate,(XtPointer)0},
  46.  
  47. };
  48. #undef Offset
  49.  
  50. /*---------------------------------------------------------------*/
  51. /* Static initialisation of the class record                     */
  52. /*---------------------------------------------------------------*/
  53.  
  54. SGMLFormattedTextClassRec  sGMLFormattedTextClassRec = {
  55.     {
  56.     (WidgetClass) &sGMLTextClassRec,     /* superclass            */
  57.     "SGMLFormattedText",                 /* class_name            */
  58.     sizeof(SGMLFormattedTextRec),        /* widget_size           */
  59.     NULL,                                /* class_initialize      */
  60.     NULL,                                /* class_part_initialize */
  61.     FALSE,                               /* class_inited          */
  62.     Initialize,                          /* initialize            */
  63.     NULL,                                /* initialize_hook       */
  64.     NULL,                                /* obj1                  */
  65.     NULL,                                /* obj2                  */
  66.     0,                                   /* obj3                  */
  67.     resources,                           /* resources             */
  68.     XtNumber(resources),                 /* num_resources         */
  69.     NULLQUARK,                           /* xrm_class             */
  70.     0,                                   /* obj4                  */
  71.     0,                                   /* obj5                  */
  72.     0,                                   /* obj6                  */
  73.     0,                                   /* obj7                  */
  74.     Destroy,                             /* destroy               */
  75.     NULL,                                /* obj8                  */
  76.     NULL,                                /* obj9                  */
  77.     NULL,                                /* set_values            */
  78.     NULL,                                /* set_values_hook       */
  79.     NULL,                                /* obj10                 */
  80.     NULL,                                /* get_values_hook       */
  81.     NULL,                                /* obj11                 */
  82.     XtVersion,                           /* version               */
  83.     NULL,                                /* callback private      */
  84.     NULL,                                /* obj12                 */
  85.     NULL,                                /* obj13                 */
  86.     NULL,                                /* obj14                 */
  87.     NULL,                                /* extension             */
  88.     },
  89.     {
  90.     ComputeSize,                   /* compute_size          */
  91.     AdjustSize,                   /* adjust_size           */
  92.     SGMLInheritAdjustPosition,        /* adjust_position       */
  93.     Redisplay,                           /* expose                */
  94.     SGMLInheritActivate,                 /* activate              */
  95.     Hilite,                              /* hilite                */
  96.     Contains,                            /* contains              */
  97.     SGMLInheritCallCreateCallback,       /* call_create_callback  */
  98.     SGMLInheritCallMapCallback,          /* call_map_callback     */
  99.     SGMLInheritMakeVisible,              /* make_visible          */
  100.     NULL,                                /* extension             */
  101.     },
  102.     {
  103.     NULL,                                /* ignore                */
  104.     }
  105. };
  106.  
  107.  
  108. WidgetClass sGMLFormattedTextObjectClass = (WidgetClass) &sGMLFormattedTextClassRec;
  109.  
  110. /*--------------------------------------------------------------*/
  111. /* Initialize:                                                  */
  112. /*--------------------------------------------------------------*/
  113.  
  114. static void Initialize (request, new)
  115. SGMLFormattedTextObject request, new;
  116. {
  117.    new->sgml_formatted_text.word_info = NULL;
  118.    new->sgml_formatted_text.line_info = NULL;
  119.    new->sgml_formatted_text.nwords = 0; 
  120.    new->sgml_formatted_text.nlines = 0; 
  121.    new->sgml_formatted_text.left_indent  = new->sgml_text.left_indent;
  122.    new->sgml_formatted_text.right_indent = new->sgml_text.right_indent;
  123.  
  124. }
  125.  
  126. /*--------------------------------------------------------------*/
  127. /* Destroy the widget: release all memory allocated             */
  128. /*--------------------------------------------------------------*/
  129.  
  130. static void Destroy (w)
  131. SGMLFormattedTextObject w;
  132. {
  133.    XtFree((char *) w->sgml_formatted_text.word_info);
  134.    XtFree((char *) w->sgml_formatted_text.line_info);
  135. }
  136.  
  137. /*--------------------------------------------------------------*/
  138. /* Contains:                                                    */
  139. /*--------------------------------------------------------------*/
  140.  
  141. static Widget Contains(w,x,y)
  142. SGMLFormattedTextObject w;
  143. Position x,y;
  144. {
  145.  
  146.   if (w->sgml_text.sensitive && 
  147.       w->sgml_text.begin.y < y  &&  w->sgml_text.begin.y + w->sgml_text.height > y &&
  148.       w->sgml_text.margin  < x  &&  w->sgml_text.margin  + w->sgml_text.width   > x )
  149.     {
  150.        if (w->sgml_text.begin.y == w->sgml_text.end.y)
  151.          {
  152.            return (Widget) w;
  153.          } 
  154.        else if (y > w->sgml_text.end.y)
  155.          {
  156.            return (x < w->sgml_text.end.x)?(Widget) w:NULL; 
  157.          }
  158.        else if (y > w->sgml_text.begin.y + w->sgml_text.begin.depth)
  159.          {
  160.             return (Widget) w;
  161.          }
  162.        else
  163.          {
  164.            return (x > w->sgml_text.begin.x)?(Widget) w:NULL; 
  165.          }      
  166.     }    
  167.  
  168.   return NULL;
  169. }
  170.  
  171. /*--------------------------------------------------------------*/
  172. /* Hilite:                                                      */
  173. /*--------------------------------------------------------------*/
  174.  
  175. static void Hilite(t,state)
  176. SGMLTextObject t;
  177. Boolean state;
  178. {
  179.   Display *dpy = XtDisplayOfObject((Widget) t);
  180.   Window  wind = XtWindowOfObject((Widget) t);
  181.   GC        gc = t->sgml_text.invert_gc;
  182.   
  183.   if (t->sgml_text.begin.y == t->sgml_text.end.y)
  184.     {
  185.       Position x = t->sgml_text.begin.x; 
  186.       Position y = t->sgml_text.begin.y; 
  187.       Dimension width  = t->sgml_text.width;
  188.       Dimension height = t->sgml_text.height;
  189.  
  190.       XFillRectangle(dpy,wind,gc,x,y,width,height);
  191.     }
  192.   else
  193.     {
  194.       XPoint p[8];
  195.  
  196.       p[0].x = t->sgml_text.begin.x;
  197.       p[0].y = t->sgml_text.begin.y;
  198.       p[1].x = t->sgml_text.margin + t->sgml_text.width;
  199.       p[1].y = p[0].y;
  200.       p[2].x = p[1].x;
  201.       p[2].y = t->sgml_text.end.y;
  202.       p[3].x = t->sgml_text.end.x;
  203.       p[3].y = p[2].y;
  204.       p[4].x = p[3].x;
  205.       p[4].y = t->sgml_text.end.y + t->sgml_text.end.depth;
  206.       p[5].x = t->sgml_text.margin;
  207.       p[5].y = p[4].y;
  208.       p[6].x = p[5].x;
  209.       p[6].y = t->sgml_text.begin.y + t->sgml_text.begin.depth;
  210.       p[7].x = p[0].x;
  211.       p[7].y = p[6].y;
  212.       
  213.       XFillPolygon(dpy,wind,gc,p,XtNumber(p),Nonconvex,CoordModeOrigin);
  214.     }
  215. }
  216.  
  217. /*--------------------------------------------------------------*/
  218. /* Redisplay                                                    */
  219. /*--------------------------------------------------------------*/
  220.  
  221. static void Redisplay(t,event,region)
  222. SGMLFormattedTextObject t;
  223. XEvent *event;
  224. Region region;
  225. {
  226.    Position y = t->sgml_text.begin.y;
  227.    Position margin = t->sgml_text.margin;    
  228.    Dimension width  = t->sgml_text.width;
  229.    Dimension height = t->sgml_text .height;
  230.  
  231.    if(XRectInRegion(region,margin,y,width,height) != RectangleOut)
  232.    {
  233.       Display *dpy = XtDisplayOfObject((Widget) t);
  234.       Window  wind = XtWindowOfObject((Widget) t);
  235.       GC gc =  t->sgml_text.gc;
  236.       XSegment *segs = NULL;
  237.  
  238.       Position x = t->sgml_text.begin.x;
  239.       Dimension depth = t->sgml_text.begin.depth;
  240.       Dimension ascent = t->sgml_text.begin.ascent;
  241.       WordInfo *info = t->sgml_formatted_text.word_info;
  242.       LineInfo *line = t->sgml_formatted_text.line_info; 
  243.       SGMLRendition *rendition; 
  244.       int l;
  245.  
  246.       if (t->sgml_text.sensitive) rendition = &t->sgml_text.sensitive_rendition;
  247.       else                        rendition = &t->sgml_text.normal_rendition;
  248.  
  249.       for( l = 0 ; l < t->sgml_formatted_text.nlines; )
  250.       {
  251.         if (XRectInRegion(region,margin,y,width,depth) != RectangleOut &&
  252.             line[l].nchars > 0)
  253.         { 
  254.           int m;
  255.           char *p = XtMalloc(line[l].nchars);
  256.           char *q = p;
  257.           for ( m = line[l].start ; m < line[l].stop ; m++)
  258.           {
  259.             strncpy(p,info[m].word,info[m].length);
  260.             p += info[m].length;
  261.             *p++ = ' ';
  262.           }  
  263.           *--p = '\0'; 
  264.           XDrawString(dpy, wind,gc,
  265.                       x, y+ascent,
  266.                       q,line[l].nchars-1);
  267.  
  268.           if (rendition->underline)
  269.             {
  270.               int s;
  271.               Position yy = y+ascent; 
  272.  
  273.               if (!segs) segs = (XSegment *) XtMalloc(sizeof(XSegment) * rendition->underline);
  274.  
  275.               for (s=0; s < rendition->underline ; s++ )
  276.                 {
  277.                    yy += max(rendition->underline_height,1); 
  278.                    segs[s].x1 = x;  
  279.                    segs[s].x2 = line[l].size;
  280.                    segs[s].y1 = yy;  
  281.                    segs[s].y2 = yy;  
  282.                    yy += max(rendition->underline_height,1); 
  283.                 }
  284.               XDrawSegments(dpy,wind,gc,segs,rendition->underline);
  285.             }
  286.           if (rendition->outline)
  287.             {
  288.               XDrawRectangle(dpy,wind,gc,x,y,line[l].size-x,depth-1);
  289.             }
  290.           XtFree(q); 
  291.         }   
  292.         x = margin;
  293.         y += depth;   
  294.  
  295.         if (l++ == t->sgml_formatted_text.nlines) break;
  296.         else if ( l+1 == t->sgml_formatted_text.nlines)
  297.           {
  298.             depth = t->sgml_text.end.depth;
  299.             ascent = t->sgml_text.end.ascent;
  300.           }
  301.         else
  302.           {
  303.             depth = t->sgml_text.depth;
  304.             ascent = t->sgml_text.ascent;
  305.           }
  306.       }
  307.      XtFree((char *) segs);
  308.    }
  309. }
  310. /*--------------------------------------------------------------*/
  311. /* Adjust Size:                                                 */
  312. /*--------------------------------------------------------------*/
  313.  
  314. static Boolean AdjustSize(w,y,ascent,depth)
  315. SGMLFormattedTextObject w;
  316. Position y;
  317. Dimension ascent, depth;
  318. {
  319.    if (w->sgml_text.end.y != y) return FALSE;
  320.      
  321.    w->sgml_text.end.ascent = ascent;
  322.    w->sgml_text.height += depth - w->sgml_text.end.depth; 
  323.    w->sgml_text.end.depth = depth;
  324.  
  325.    if (w->sgml_text.begin.y == y)
  326.      {
  327.        w->sgml_text.begin.ascent = ascent;
  328.        w->sgml_text.begin.depth = depth;
  329.        return TRUE;
  330.      }
  331.         
  332.    return FALSE;
  333. }   
  334. /*--------------------------------------------------------------*/
  335. /* Compute Size:                                                */
  336. /*--------------------------------------------------------------*/
  337.  
  338. static void ComputeSize(w,geom,Adjust,Closure)
  339. SGMLFormattedTextObject w;
  340. SGMLGeometry *geom;
  341. AdjustSizeProc Adjust;
  342. Opaque Closure;
  343. {
  344.    Dimension left_clearance  = w->sgml_text.left_margin  + w->sgml_formatted_text.left_indent;
  345.    Dimension right_clearance = w->sgml_text.right_margin + w->sgml_formatted_text.right_indent;
  346.    Boolean punctuation;
  347.    char *punc_char = ".,:;)"; 
  348.    Position xmin, xmax;
  349.  
  350. /* If we haven't already calculated the size of the words, do so now. */
  351.  
  352.    if (w->sgml_formatted_text.word_info == NULL) WordSize(w); 
  353.  
  354.    /*
  355.     * Break before?
  356.     */  
  357.  
  358.    if ( w->sgml_text.break_before == SGMLBREAK_ALWAYS ||
  359.        (w->sgml_text.break_before == SGMLBREAK_SOFT && geom->coord.x > left_clearance) ||
  360.        (w->sgml_text.alignment != geom->alignment && geom->alignment))
  361.    {
  362.       _SGMLBreak(geom,w->sgml_text.space_before);
  363.    } 
  364.  
  365.   
  366. /*
  367.  *  We will leave a space before the first word if:
  368.  *       a) Not beginning a new line
  369.  *       b) Not a punctuation character
  370.  *       c) the geometry structure requests it.
  371.  */
  372.  
  373.    punctuation = !w->sgml_formatted_text.word_info || strchr(punc_char,*w->sgml_formatted_text.word_info[0].word);
  374.   
  375.    if (!punctuation) 
  376.      if (geom->coord.x + w->sgml_text.spacing + w->sgml_formatted_text.word_info[0].size + right_clearance > geom->natural_width)
  377.        {
  378.           geom->coord.x = left_clearance;
  379.           geom->coord.y += geom->coord.depth;
  380.           geom->coord.depth = w->sgml_text.depth;
  381.           geom->coord.ascent = w->sgml_text.ascent;
  382.        }
  383.      else if (geom->leave_space) geom->coord.x += w->sgml_text.spacing;   
  384.  
  385.    if (w->sgml_text.depth  > geom->coord.depth  ||
  386.        w->sgml_text.ascent > geom->coord.ascent )
  387.      {  
  388.        if (w->sgml_text.depth  > geom->coord.depth)  geom->coord.depth  =  w->sgml_text.depth;
  389.        if (w->sgml_text.ascent > geom->coord.ascent) geom->coord.ascent =  w->sgml_text.ascent;
  390.        
  391.        Adjust(Closure, geom->coord.y, geom->coord.ascent, geom->coord.depth);
  392.      }  
  393.  
  394.    if (left_clearance > geom->coord.x && geom->broken) geom->coord.x = left_clearance;
  395.  
  396.    w->sgml_text.begin = geom->coord;
  397.  
  398.    LineSize(w,geom,&xmin,&xmax);
  399.  
  400.    if (xmax > geom->actual_width) geom->actual_width = xmax;
  401.    
  402.    w->sgml_text.width  = xmax - xmin;
  403.    w->sgml_text.margin = xmin;  
  404.    w->sgml_text.height = geom->coord.y + geom->coord.depth - w->sgml_text.begin.y;
  405.  
  406.    w->sgml_text.end = geom->coord;
  407.  
  408.    /*
  409.     * Break after?
  410.     */  
  411.  
  412.    if (w->sgml_text.break_after)
  413.    {
  414.      _SGMLBreak(geom,w->sgml_text.space_after);
  415.    } 
  416. }
  417.  
  418. /*-----------------------------------------------------------------------*/
  419. /* Private routines                                                      */
  420. /*-----------------------------------------------------------------------*/
  421. /* WordSize: Build an array giving the length of each word               */
  422. /*-----------------------------------------------------------------------*/
  423.  
  424. static void WordSize(w)
  425. SGMLFormattedTextObject w;
  426. {
  427.    int nwords;
  428.    char *p = w->sgml_text.text;
  429.    char *delim  = " \t\n"; 
  430.    WordInfo *info = w->sgml_formatted_text.word_info;
  431.    XFontStruct *font;
  432.    
  433.    if (w->sgml_text.sensitive) font=w->sgml_text.sensitive_rendition.font;
  434.    else                        font=w->sgml_text.normal_rendition.font; 
  435.    
  436.    /* count how many words */
  437.    
  438.    for (nwords=0; *p != '\0';nwords++) 
  439.    {
  440.      for (; *p != '\0' && strchr(delim,*p);  p++); /* scan white space */
  441.      if (*p == '\0') break;
  442.       
  443.      for (; !strchr(delim,*p); p++); 
  444.    }
  445.    
  446.    /* Allocate storage for word statistics */
  447.  
  448.    XtFree((char *) info); 
  449.    info = nwords?(WordInfo *) XtMalloc(sizeof(WordInfo) * nwords):NULL; 
  450.  
  451.    /* Now store length of each word */
  452.    
  453.    p = w->sgml_text.text;   
  454.    for (nwords=0; *p != '\0' ; nwords++) 
  455.    {
  456.      for (; *p != '\0' && strchr(delim,*p);  p++); /* scan white space */
  457.      if (*p == '\0') break;
  458.      
  459.      info[nwords].word = p;
  460.      for (; !strchr(delim,*p); p++);
  461.      info[nwords].length = p - info[nwords].word;
  462.  
  463.      info[nwords].size = XTextWidth(font,info[nwords].word,info[nwords].length);
  464.    }
  465.    w->sgml_formatted_text.word_info = info;
  466.    w->sgml_formatted_text.nwords = nwords;
  467.  
  468. }
  469. /*-----------------------------------------------------------------------*/
  470. /* LineSize: Build an array giving the length of each line               */
  471. /*-----------------------------------------------------------------------*/
  472.  
  473. static void LineSize(w, geom, xmin, xmax)
  474. SGMLFormattedTextObject w;
  475. SGMLGeometry *geom;
  476. Position *xmin, *xmax; 
  477. {
  478.   Position x = geom->coord.x;
  479.   Position y = geom->coord.y; 
  480.   Position maxX = x;
  481.   Position minX = x; 
  482.   int n = 0;  
  483.   int l = 0;
  484.   int nc = 0; 
  485.   int lmax = 10;
  486.   Dimension size = 0;  
  487.   Dimension spacing = 0;
  488.   WordInfo *info = w->sgml_formatted_text.word_info;
  489.   Dimension left_clearance  = w->sgml_text.left_margin  + w->sgml_formatted_text.left_indent;
  490.   Dimension right_clearance = w->sgml_text.right_margin + w->sgml_formatted_text.right_indent;
  491.   
  492.   LineInfo *line = (LineInfo *) XtMalloc(lmax*sizeof(LineInfo)); 
  493.  
  494.   XtFree((char *) w->sgml_formatted_text.line_info);
  495.      
  496.   line[l].start = n;
  497.  
  498.   for (; n < w->sgml_formatted_text.nwords; n++)
  499.   {
  500.     if (spacing>0 && x + spacing + info[n].size + right_clearance > geom->natural_width)
  501.     {
  502.       line[l].stop = n;
  503.       line[l].nchars = nc;  
  504.       line[l].size = x;
  505.       if (x > maxX) maxX = x;
  506.  
  507.       if (++l == lmax) line = (LineInfo *) XtRealloc((char *) line, (lmax *= 2)*sizeof(LineInfo));
  508.  
  509.       line[l].start = n;
  510.       x = left_clearance + info[n].size;
  511.       y += geom->coord.depth;
  512.       geom->coord.depth = w->sgml_text.depth;
  513.       geom->coord.ascent = w->sgml_text.ascent;
  514.       minX = left_clearance;
  515.       nc = info[n].length + 1; 
  516.     }  
  517.     else 
  518.     {
  519.       x += spacing + info[n].size;
  520.       nc += info[n].length + 1;
  521.     }
  522.     geom->alignment = w->sgml_text.alignment; 
  523.     geom->broken = FALSE;
  524.     geom->leave_space = TRUE;
  525.     spacing =  w->sgml_text.spacing;
  526.   }
  527.   line[l].stop = n;
  528.   line[l].nchars = nc;  
  529.   line[l].size = x;
  530.   if (x > maxX) maxX = x;
  531.   
  532.   w->sgml_formatted_text.line_info = line;
  533.   w->sgml_formatted_text.nlines = l + 1;
  534.  
  535.   geom->coord.x = x; 
  536.   geom->coord.y = y;  
  537.   *xmin = minX; 
  538.   *xmax = maxX;  
  539.  
  540. }
  541. /*-----------------------------------------------------------------------*/
  542. /* Create a new SGMLFormattedTextObject                                  */
  543. /*-----------------------------------------------------------------------*/
  544.  
  545. Widget SGMLCreateFormattedText(parent,name,al,ac)
  546. Widget parent;
  547. char   *name;
  548. ArgList al;
  549. int     ac;
  550. {
  551.     return XtCreateWidget(name,sGMLFormattedTextObjectClass,parent,al,ac);
  552. }
  553.